package com.nullpointer.xchat.utils;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.nullpointer.xchat.object.BlockUser;
import com.nullpointer.xchat.object.RoomType;
import com.nullpointer.xchat.object.User;
import com.nullpointer.xchat.object.UserInfo;
import com.nullpointer.xchat.object.UserInfo.GenderType;

public class DataManager {
	private static DataManager mDataManager;
	private SharedPreferences pref;
	// private SharedPreferences prefContact;
	private Editor editor;
	// public static final String REPOSITORY_CONTACT = "Couple_screen_contact";
	public static final String DEVICE_ID = "device_id";
	public static final String USER_ID = "user_id";

	public static final String CURRENT_CHAT_USER_ID = "current_chat_user_id";

	public static final String USER_NAME = "user_name";
	public static final String USER_STATUS = "user_status";
	public static final String USER_BIRTHDAY = "USER_BIRTHDAY";
	public static final String USER_GENDER = "user_gender";
	public static final String IS_LOGOUT = "is_logout";
	public static final String CURRENT_ROOM = "current_room";
	public static final String IS_SHOW_POST = "IS_SHOW_POST";
	public static final String IS_SHOW_NOTIFY = "IS_SHOW_NOTIFY";

	public static final String NOTIFY_USER_ID = "NOTIFY_USER_ID";
	public static final String NOTIFY_USER_NAME = "NOTIFY_USER_NAME";
	public static final String LAST_AUTO_USER_INDEX = "LAST_AUTO_USER_INDEX";

	// SETTING
	public static final String NOTIFICATION_SETTING = "NOTIFICATION_SETTING";
	public static final String SOUND_SETTING = "SOUND_SETTING";
	public static final String VIBRATION_SETTING = "VIBRATION_SETTING";
	public static final String INVISIBLE_SETTING = "INVISIBLE_SETTING";

	// FIND FRIEND SETTING
	public static final String AGE_MIN = "AGE_MIN";
	public static final String AGE_MAX = "AGE_MAX";
	public static final String GENDER_SEARCH = "GENDER_SEARCH";

	// RATE
	public static final String IS_RATED = "IS_RATED";

	// BLOCK USER
	public static final String BLOCK_USER = "BLOCK_USER";

	public DataManager(Context context) {
		pref = PreferenceManager.getDefaultSharedPreferences(context);
		// prefContact = context.getSharedPreferences(REPOSITORY_CONTACT,
		// Context.MODE_PRIVATE);
		editor = pref.edit();
		// editorContact = prefContact.edit();
	}

	public static DataManager getInstance(Context context) {
		if (mDataManager == null) {
			mDataManager = new DataManager(context);
		}
		return mDataManager;
	}

	// public void setCurrentContactID(String id){
	// editor.putString(CURRENT_DEVICE_ID, id);
	// editor.commit();
	// }
	//
	// public String getCurrentContactID(){
	// return pref.getString(CURRENT_DEVICE_ID, null);
	// }

	public void setDeviceID(String id) {
		editor.putString(DEVICE_ID, id);
		editor.commit();
	}

	public String getDeviceID() {
		return pref.getString(DEVICE_ID, null);
	}

	public void setUserID(Integer id) {
		editor.putInt(USER_ID, id == null ? 0 : id);
		editor.commit();
	}

	public Integer getUserID() {
		int value = pref.getInt(USER_ID, 0);
		return (value == 0) ? null : value;
	}

	public void setUserName(String name) {
		editor.putString(USER_NAME, name);
		editor.commit();
	}

	public String getUserName() {
		return pref.getString(USER_NAME, null);
	}

	public void setUserBirthday(String birthday) {
		editor.putString(USER_BIRTHDAY, birthday);
		editor.commit();
	}

	public String getUserBirthday() {
		return pref.getString(USER_BIRTHDAY, "1990/01/01");
	}

	public void setUserStatus(String status) {
		editor.putString(USER_STATUS, status);
		editor.commit();
	}

	public String getUserStatus() {
		return pref.getString(USER_STATUS, "Hi");
	}

	public void setCurrentChatUserId(Integer userID) {
		editor.putInt(CURRENT_CHAT_USER_ID, userID == null ? 0 : userID);
		editor.commit();
	}

	public Integer getCurrentChatUserId() {
		int value = pref.getInt(CURRENT_CHAT_USER_ID, 0);
		return (value == 0) ? null : value;
	}

	public boolean isShowPost() {
		return pref.getBoolean(IS_SHOW_POST, false);
	}

	public void setIsShowPost(boolean isShowPost) {
		editor.putBoolean(IS_SHOW_POST, isShowPost);
		editor.commit();
	}

	public boolean isLogout() {
		return pref.getBoolean(IS_LOGOUT, true);
	}

	public void setIsLogout(boolean isLogout) {
		editor.putBoolean(IS_LOGOUT, isLogout);
		editor.commit();
	}

	public boolean isShowNotify() {
		return pref.getBoolean(IS_SHOW_NOTIFY, false);
	}

	public void setIsShowNotify(boolean isShowNotify) {
		editor.putBoolean(IS_SHOW_NOTIFY, isShowNotify);
		editor.commit();
	}

	public void setCurrentRoom(RoomType roomType) {
		editor.putInt(CURRENT_ROOM, roomType.ordinal());
		editor.commit();
	}

	public RoomType getCurrentRoom() {
		return RoomType.values()[pref.getInt(CURRENT_ROOM, 0)];
	}

	public void setUserGender(GenderType genderType) {
		editor.putInt(USER_GENDER, genderType.ordinal());
		editor.commit();
	}

	public GenderType getUserGender() {
		return GenderType.values()[pref.getInt(USER_GENDER, 0)];
	}

	public User getUser() {
		User user = new User();
		user.setUserID(getUserID());
		user.setName(getUserName());
		user.setUserInfo(getUserInfo());
		return user;
	}

	public UserInfo getUserInfo() {
		UserInfo userInfo = new UserInfo();
		userInfo.setGender(getUserGender());
		userInfo.setStatus(getUserStatus());
		return userInfo;
	}

	public void setNotifyUserId(Integer userID) {
		editor.putInt(NOTIFY_USER_ID, (userID == null) ? 0 : userID);
		editor.commit();
	}

	public Integer getNotifyUserId() {
		int value = pref.getInt(NOTIFY_USER_ID, 0);
		return (value == 0) ? null : value;
	}

	public void setNotifyUserName(String userName) {
		editor.putString(NOTIFY_USER_NAME, userName);
		editor.commit();
	}

	public String getNotifyUserName() {
		return pref.getString(NOTIFY_USER_NAME, null);
	}

	public int getLastAutoUserIndex() {
		return pref.getInt(LAST_AUTO_USER_INDEX, 0);
	}

	public void setLastAutoUserIndex(int index) {
		editor.putInt(LAST_AUTO_USER_INDEX, index);
		editor.commit();
	}

	public boolean getNotificationSetting() {
		return pref.getBoolean(NOTIFICATION_SETTING, true);
	}

	public void setNotificationSetting(boolean isOn) {
		editor.putBoolean(NOTIFICATION_SETTING, isOn);
		editor.commit();
	}

	public boolean getSoundSetting() {
		return pref.getBoolean(SOUND_SETTING, true);
	}

	public void setSoundSetting(boolean isOn) {
		editor.putBoolean(SOUND_SETTING, isOn);
		editor.commit();
	}

	public boolean getVibrationSetting() {
		return pref.getBoolean(VIBRATION_SETTING, true);
	}

	public void setVibrationSetting(boolean isOn) {
		editor.putBoolean(VIBRATION_SETTING, isOn);
		editor.commit();
	}

	public boolean getInvisibleSetting() {
		return pref.getBoolean(INVISIBLE_SETTING, false);
	}

	public void setInvisibleSetting(boolean isDontShow) {
		editor.putBoolean(INVISIBLE_SETTING, isDontShow);
		editor.commit();
	}

	public boolean isRatedApp() {
		return pref.getBoolean(IS_RATED, false);
	}

	public void setRatedApp(boolean isRatedApp) {
		editor.putBoolean(IS_RATED, isRatedApp);
		editor.commit();
	}

	public void setAgeMin(int value) {
		editor.putInt(AGE_MIN, value);
		editor.commit();
	}

	public int getAgeMin() {
		return pref.getInt(AGE_MIN, 18);
	}

	public void setAgeMax(int value) {
		editor.putInt(AGE_MAX, value);
		editor.commit();
	}

	public int getAgeMax() {
		return pref.getInt(AGE_MAX, 50);
	}

	public void setGenderSearch(GenderType genderType) {
		editor.putInt(GENDER_SEARCH, genderType.ordinal());
		editor.commit();
	}

	public GenderType getGenderSearch() {
		return GenderType.values()[pref.getInt(GENDER_SEARCH,
				GenderType.ANY.ordinal())];
	}

	public Set<Integer> getBlockUsers() {
		String listString = pref.getString(BLOCK_USER, null);
		if (listString != null) {
			return BlockUser.fromJsonToBlockUser(listString).getBlockUsers();
		}
		return new HashSet<Integer>();
	}

	public void addBlockUsers(Integer userID) {
		if (userID == null) {
			return;
		}
		Set<Integer> blockUsers = getBlockUsers();
		blockUsers.add(userID);
		BlockUser blockUser = new BlockUser();
		blockUser.setBlockUsers(blockUsers);
		editor.putString(BLOCK_USER, blockUser.toJson());
		editor.commit();
	}

	public void removeBlockUsers(Integer userID) {
		if (userID == null) {
			return;
		}
		Set<Integer> blockUsers = getBlockUsers();
		blockUsers.remove(userID);
		BlockUser blockUser = new BlockUser();
		blockUser.setBlockUsers(blockUsers);
		editor.putString(BLOCK_USER, blockUser.toJson());
		editor.commit();
	}
}
